<?php

namespace Addons\IntegralShop\Repositor;

use Addons\IntegralShop\Model\IsGoodModel;

class IsGoodRepositor extends BaseRepositor
{

    private $model;

    public function __construct()
    {
        $this->model = new IsGoodModel();
    }

    /**
     * 商品列表
     * @param $page
     * @param $size
     * @return mixed
     */
    public function lists($page, $size)
    {
        $map['mpid'] = get_mpid();
        $map['is_deleted'] = 0;
        $list = $this->model->where($map)->page($page, $size)->order('id desc')->select();

        $orderRepo = new IsOrderRepositor();
        foreach ($list as &$item) {
            // 获取商品剩余数量
            $item['number'] = $item['number'] - $orderRepo->getOrderNumByGoodId($item['id']);
        }

        return $list;
    }

    public function detail($id){
        $info = $this->model->getOne($id);
        if(empty($info)){
            $this->errMsg = sprintf("商品: %d 不存在",$id);
            return false;
        }

        return $info;
    }

    /**
     * 删除商品(伪删除)
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        return $this->model->where('id', $id)->setField('is_deleted', 1);
    }
}
